package com.zxt.dental_sys.Controller.Reservation;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zxt.dental_sys.model.ReservationInfo;
import com.zxt.dental_sys.model.vo.RestBean;
import com.zxt.dental_sys.service.impl.ReservationInfoServiceImpl;
import com.zxt.dental_sys.service.impl.SysElementUserinfoServiceImpl;
import com.zxt.dental_sys.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/reservation")
public class ReservationController {

    @Resource
    ReservationInfoServiceImpl reservationInfoService;

    @Resource
    JwtUtil jwtUtil;
    @Autowired
    private SysElementUserinfoServiceImpl sysElementUserinfoServiceImpl;

    @RequestMapping("add")
    public RestBean add(@RequestBody ReservationInfo reservationInfo, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        reservationInfo.setPatientUserId(uuid);
        reservationInfo.setInputTime(new Date());
        reservationInfo.setStatus(1);//2 为用户取消
        reservationInfo.setReservationId(UUID.randomUUID().toString());
        try {
            long count = reservationInfoService.count(
                    new QueryWrapper<ReservationInfo>()
                            .eq("patient_user_id", uuid)
                            .ge("input_time", LocalDate.now().atStartOfDay()) // 今天 00:00:00
                            .lt("input_time", LocalDate.now().plusDays(1).atStartOfDay()) // 明天 00:00:00
            );
            if(count >0l){
                return RestBean.failure(400,"今天已预约过");
            }
        }catch (Exception e){

        }
        return reservationInfoService.save(reservationInfo)?
                RestBean.success(reservationInfo):RestBean.failure(500,"预约失败");
    }

    @RequestMapping("/listofmy")
    public RestBean listOfMy(HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        List<ReservationInfo> list = reservationInfoService.list(new QueryWrapper<ReservationInfo>()
                .eq("patient_user_id", uuid)
                .eq("is_deleted", 0)
        );
        return RestBean.success(list);
    }

    @RequestMapping("/list/{page}/{size}")
    public RestBean list(@PathVariable int page,@PathVariable int size){
        PageMethod.startPage(page,size);
        List<ReservationInfo> list = reservationInfoService.list(new QueryWrapper<ReservationInfo>()
                .eq("is_deleted", 0)
                .orderByDesc("input_time")
        );
        list.forEach(el->{
            el.setPatientUserId(JSON.toJSONString(sysElementUserinfoServiceImpl.getById(el.getPatientUserId())));
        });
        return RestBean.success(new PageInfo(list));
    }

    @RequestMapping("del/{id}")
    public RestBean del(@PathVariable String id){
        try {
            ReservationInfo byId = reservationInfoService.getById(id);
            if (byId == null){
                throw new Exception();
            }
            byId.setIsDeleted(1);
            return reservationInfoService.updateById(byId)?
                    RestBean.success(byId):RestBean.failure(500,"服务异常");
        }catch (Exception e){
            return RestBean.failure(400,"记录异常");
        }
    }


}
